import { Window_Selectable } from "@/windows/Window_Selectable.js";

export class Window_BattleTargets extends Window_Selectable{
    constructor(width,height){
        super(width,height,false);
        this.init(width,height);
    }

    init(width,height){
        this.action=null;
        this.item=null;
        this.list=[];
        this.okHandler=null;
        this.cancelHandler=null;
        super.init(width,height);
    }

    destroy(opts){
        super.destroy(opts);
        this.okHandler=null;
        this.cancelHandler=null;
    }

    setHandler(name,cb){
        if(name=='ok')this.okHandler=cb;
        else if(name=='cancel')this.cancelHandler=cb;
    }

    onResponse(index){
        this.okHandler?.(this.list[index]);
        this.list[index]?.deselect();
    }
    onCancel(){
        this.cancelHandler?.();
        this.list[this.selectedIndex]?.deselect();
    }
    onSelectChanged(index,oldIndex){
        this.list[oldIndex]?.deselect();
        this.list[index]?.select();
    }


    column(){return 2;}
    viewColumn(){return 2;}
    row(){return (this.length()+1)>>1;}
    viewRow(){return Math.floor(this.contentsHeight()/this.viewColumn());}
    length(){return this.list.length;}

    itemWidth(){return this.contentsWidth()/this.viewColumn();}

    drawItem(index,x,y,width,height){
        let item=this.list[index];
        if(item.isEnemy()){
            this.changeTextColor('#ff0000');
        }else if(item.isActor()){
            this.changeTextColor('#00ff00');
        }else{
            this.changeTextColor('#ffffff');
        }
        this.drawText(item.battlerName(),x,y,width,height);
        if(item.isDead()){
            this.drawLine(x,y,x+width,t+height,'#ff0000');
            this.drawLine(x,y+height,x+width,y,'#ff0000');
        }
    }

    setAction(action){
        if(!action)this.contents.clear();
        this.action=action;
        this.item=this.action.item();
        if(this.action.isForOpponent()){
            this.list=$gameTroop.members();
        }else if(this.action.isForFriend()){
            if(this.action.isForDeadFriend()){
                this.list=$gameParty.deadMembers();
            }else{
                this.list=$gameParty.aliveMembers();
            }
        }else{
            this.list=$gameParty.members().concat($gameTroop.members());
        }
        this.refresh();
    }
}